<?php

/*

	Copyright © 2011 Mike Bennett <mike@kereru.com>

	This file is part of Tazu-Time.

    Tazu-Time is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    Tazu-Time is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tazu-Time.  If not, see <http://www.gnu.org/licenses/>.
    
*/



class edit_customer  {

	var $title = FALSE;
	var $name = 'edit_customer';
	var $entry = FALSE;

	var $customer_id = FALSE;
	var $db_file;

	function __construct($title="Edit_Customer",$name=FALSE) {
		$this->title = $title;	
		if ( $name ) $this->name = $name;
	}


	function edit($customer_id,$db_file=FALSE) {

		// $customer_id == FALSE add new record

		$this->customer_id = $customer_id;
		$this->db_file = $db_file;
		if ( ! $db_file ) $this->db_file = platform::app_data('Tazu-Timesheet-Manager').'customers.db';

		// label is above the text entry
		// prompt is to the left
		// err_msg is below

		$glade = new GladeXML(PATH_RES.'edit_customer.glade','edit_customer');
		$dialog = $glade->get_widget('edit_customer');
		$dialog->set_title($this->title);

//		$this->widget->set_label(settings::xlate($this->widget->get_label()));

		$labels = array('name_lbl','company_lbl','email_lbl');
		foreach ( $labels as $lbl ) {
			$w = $glade->get_widget($lbl);
			$w->set_label(settings::xlate($w->get_label()));
		}		


		$n = $glade->get_widget('tse_customer_name');
		$c = $glade->get_widget('tse_company_name');
		$e = $glade->get_widget('tse_email_bill');


		$name = '';
		$company = '';
		$email = '';

		// Edit an existing customer record
		if ( $this->customer_id ) {
			$ts = new Customers($this->db_file);

printf("[%s][%s]\n",$customers_db,$this->customer_id);

			$name = $ts->getCustomerName($this->customer_id);
			$company = $ts->getCustomerCompany($this->customer_id);
			$email_bill = $ts->col($this->customer_id,'customers','email',$ts->cust_dsn);
			$ts->close();
			unset($ts);
		}
			
		$n->set_text($name);
		$c->set_text($company);
		$e->set_text($email_bill);


//		$this->entry = $glade->get_widget('entry');
//		$this->entry->set_width_chars($width_chars);


        $buttons = $dialog->action_area->get_children();
        $button_ok = $buttons[1]; // get the ID of the OK button

        // simulate button click when user press enter
/*
		$this->entry->set_text($default);
        $this->entry->connect('activate', array(&$this, 'on_enter'), $button_ok);
*/

        $dialog->show_all();
        $result = $dialog->run();

		//echo "+ ".Gtk::RESPONSE_OK."\n";
		//echo "- ".Gtk::RESPONSE_CANCEL."\n";
		//echo "->[$result]\n";

		if ( $result == Gtk::RESPONSE_OK ) {

			$name = $n->get_text();
			$company = $c->get_text();
			$email_bill = $e->get_text();
			

			$ok = TRUE;			// Need validation
			if ( $ok ) {
					
				$ts = new Customers($this->db_file);

				if ( $this->customer_id ) {
					// Existing record
					$ts->updateCustomerName($this->customer_id,$name);							
					$ts->updateCompanyName($this->customer_id,$company);							
					$ts->updateCustomer($this->customer_id,'email',$email_bill);							
				}
				else {
					// New record
					$customer_id = $ts->addCustomer($name,$company,$email_bill);
				}
						
				$ts->close();
				unset( $ts );
			}


			$result = $customer_id;

//			$data = $this->entry->get_text();

			// save last selected date
//			$cnf->set_value('DLG_LAST',$this->name,$data);
//			$cnf->save_ini();

		}
		else {

			$result = FALSE;		// cancel
		}

        $dialog->destroy();

		return($result);

    }


	// simulate button click when user press enter
    function on_enter($entry, $button) {
        $button->clicked();
    }

}

?>